<?php
    $this->layout('base.phtml', [
        'title' => _('Repairing a link'),
        'canonical' => url_full('repairing link', ['id' => $link->id]),
        'has_errors' => $form->isInvalid(),
        'modal_enabled' => true,
    ]);
?>

<div class="section">
    <div class="section__title">
        <h1 id="modal-title"><?= _('Repairing a link') ?></h1>
    </div>

    <p class="section__intro">
        <?= _('You can change the <abbr>URL</abbr> of a link if it’s broken, or if you made a mistake in it.') ?>
    </p>

    <form data-turbo-preserve-scroll method="post" action="<?= url('repair link', ['id' => $link->id]) ?>">
        <?= $this->include('alerts/_error.phtml', ['message' => $form->error('@base')]) ?>

        <div
            class="form-group"
            data-controller="link-suggestion"
            data-link-suggestion-url-value="<?= protect($form->urlCleared()) ?>"
        >
            <label for="url">
                <?= _('What’s the address of the link?') ?>
            </label>

            <?php if ($form->isInvalid('url')): ?>
                <p id="url-error" class="form-group__error">
                    <?= _('Error:') ?>
                    <?= $form->error('url') ?>
                </p>
            <?php endif; ?>

            <input
                id="url"
                name="url"
                type="url"
                placeholder="https://…"
                required
                value="<?= protect($form->url) ?>"
                autocomplete="off"
                autofocus
                data-link-suggestion-target="input"
                <?php if ($form->hasDetectedTrackers()): ?>
                    aria-describedby="trackers-detected-caption"
                <?php endif; ?>
                <?php if ($form->isInvalid('url')): ?>
                    aria-errormessage="url-error"
                    aria-invalid="true"
                <?php endif; ?>
            />

            <?php if ($form->hasDetectedTrackers()): ?>
                <p
                    id="trackers-detected-caption"
                    class="form-group__error"
                    data-link-suggestion-target="message"
                >
                    <?php if ($form->urlCleared()): ?>
                        <?= _f('%s has detected trackers in this <abbr>URL</abbr>. Suggestion:', get_app_configuration('brand')) ?>
                        <?= $form->urlCleared() ?>

                        <button
                            type="button"
                            class="button--small"
                            data-action="link-suggestion#change"
                        >
                            <?= _('Ok') ?>
                        </button>
                    <?php else: ?>
                        <?= _f('%s has detected that this <abbr>URL</abbr> tracks users. It is suggested to delete the link.', get_app_configuration('brand')) ?>
                    <?php endif; ?>
                </p>
            <?php endif; ?>
        </div>

        <div class="form-group" data-controller="caption-switcher">
            <input
                type="checkbox"
                id="force-sync"
                name="force_sync"
                <?= $form->force_sync ? 'checked' : '' ?>
                aria-describedby="force-sync-caption"
                data-action="caption-switcher#switch"
                data-caption-switcher-target="switch"
            />

            <label class="label--checkbox" for="force-sync">
                <?= _('Force the synchronization') ?>
            </label>

            <p class="form-group__caption" id="force-sync-caption" aria-live="polite">
                <span data-caption-switcher-target="caption" data-caption-value="on">
                    <?= _('The title, reading time and illustration will be re-synchronized even if you’ve changed them.') ?>
                </span>

                <span data-caption-switcher-target="caption" data-caption-unchecked>
                    <?= _('The title, reading time and illustration will be synchronize only if you’ve never changed them.') ?>
                </span>
            </p>
        </div>

        <div class="form__actions">
            <button type="submit" class="button--primary">
                <?= _('Repair the link') ?>
            </button>
        </div>

        <input type="hidden" name="csrf_token" value="<?= csrf_token($form) ?>" />
    </form>
</div>
